package com.study;

import com.study.job.MyDataFlowJob;
import com.study.job.MySimpleJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;

/**
 * @author Mayjean
 */
public class Main {

    public static void main(String[] args) {
        JobConfiguration jobConfig = JobConfiguration.newBuilder("MyJob1", 3)
                .cron("0/5 * * * * ?")
                .shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou")
                .failover(true)
                .build();

        JobConfiguration jobConfig2 = JobConfiguration.newBuilder("MyJob2", 3)
                .cron("0/5 * * * * ?")
                .shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou")
                .failover(true)
                .build();

        new ScheduleJobBootstrap(createRegistryCenter(), new MySimpleJob(), jobConfig).schedule();
        new ScheduleJobBootstrap(createRegistryCenter(), new MyDataFlowJob(), jobConfig2).schedule();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("127.0.0.1:2181", "my-job"));
        regCenter.init();
        return regCenter;
    }
}